package com.threeti.mecool.core.domain.model.plan.pojo;

import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonManagedReference;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "DM_WF_STEP_ITEM")
public class StepItem {
    private Long id;
    private Integer version;
    private Step step;
    private TaskItem taskItem;
    private List<Step> steps = new ArrayList<Step>();

    public Step createStep(Task task, String stepName) {
        Step ret = new Step();
        ret.setStepItem(this);
        ret.setTask(task);
        ret.setName(stepName);
        steps.add(ret);
        ret.createStepItems();
        return ret;
    }


    public Step createStep(Task task, String stepName, boolean fixed, boolean optional) {
        Step ret = new Step();
        ret.setStepItem(this);
        ret.setTask(task);
        ret.setName(stepName);
        ret.setFixed(fixed ? 1 : 0);
        ret.setOptional(optional ? 1 : 0);
        steps.add(ret);
        ret.createStepItems();
        return ret;
    }

    @JsonManagedReference("stepItem")
    @OneToMany(mappedBy = "stepItem", cascade = CascadeType.ALL, orphanRemoval = true)
    public List<Step> getSteps() {
        return steps;
    }

    public void setSteps(List<Step> steps) {
        this.steps = steps;
    }

    @OneToOne
    public TaskItem getTaskItem() {
        return taskItem;
    }

    public void setTaskItem(TaskItem taskItem) {
        this.taskItem = taskItem;
    }

    @JsonBackReference("step")
    @ManyToOne
    public Step getStep() {
        return step;
    }

    public void setStep(Step step) {
        this.step = step;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

}
